<template>
  <div id="market_gameticket_times">
    <div class="search-box">
      <Row type="flex" justify="space-between">
        <Row type="flex">
          <Col class="search-item"><span class="search-label">名称:</span><Input placeholder="请输入" :maxlength="20" @on-keyup='filterSpace' @on-blur="filterSpace" v-model='keyName'></Input></Col>
          <Col class="search-item">
            <span class="search-label">状态:</span>
            <Select v-model="searchObj.activityStatus" @on-change="search"
              style="width:180px">
              <Option :value="-1">全部</Option>
              <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </Col>
          <Col class="search-item">
            <span class="search-label">场景类型:</span>
            <Select v-model="searchObj.activityIntegerType" @on-change="search"
              style="width:180px">
              <Option :value="-1">全部</Option>
              <Option v-for="item in sceneList" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                  type='primary'
                  @click="doSearch"
                  >查询</Button>
          </Col>
          <Col class="search-item">
          <Button class="search-btn"
                @click="reset">重置</Button>
          </Col>
        </Row>
        <Row type="flex">
          <Col class="search-item" style="padding-right:15px;">
           <Dropdown trigger="click" @on-visible-change="dropVisible=!dropVisible" @on-click="skipAdd">
                  <Button class="search-btn" type="primary"  v-if="hasPermission('moms:market:pointscenecreate')">创建
                    <Icon :type="dropVisible ? 'ios-arrow-up' : 'ios-arrow-down'"></Icon>
                  </Button>
                  <DropdownMenu slot="list">
                    <DropdownItem  name="4">消费场景</DropdownItem>
                    <DropdownItem  name="5">完善资料</DropdownItem>
                  </DropdownMenu>
                </Dropdown>
          </Col>
        </Row>
      </Row>
    </div>
    <Table stripe class="table-list" :columns="columns" :data="data" :loading="loading" :height="this.tableHeight">
      <template slot-scope="{ row }" slot="activityIntegerType">
        {{ getEnumsName(row.activityIntegerType, sceneList) }}
      </template>
      <template slot-scope="{ row }" slot="activityStatus">
        {{ getEnumsName(row.activityStatus, statusList) }}
      </template>
      <template slot-scope="{ row }" slot="action">
        <i class="iconsys icondetail" title="查看" @click='skipDetail(row)' v-if="hasPermission('moms:market:pointscenedetail')"></i>
        <i class="iconsys iconedit" title="编辑" @click='skipEdit(row)' v-if="hasPermission('moms:market:pointsceneupdate') && row.activityStatus == 0"></i>
        <Poptip confirm title="你确定要删除吗？" transfer @on-ok="sureDel(row)"
        v-if="hasPermission('moms:market:pointscenedelete') && row.activityStatus == 0"
        style="text-align:left">
          <i class="iconsys icondel" title="删除"></i>
        </Poptip>
        <Poptip confirm title="你确定要立即结束吗？" transfer @on-ok="skipOver(row)"
        v-if="hasPermission('moms:market:pointsceneclose') && row.activityStatus == 1"
        style="text-align:left">
          <i class="iconsys iconxiajia" title="立即结束"></i>
        </Poptip>
      </template>
    </Table>
    <Row type='flex' justify="end" style='padding: 20px'>
      <Col>
        <Page :total="total"
              :page-size='pageSize'
              :current='pageIndex'
              show-total
              show-sizer
              @on-change='changePageIndex'
              @on-page-size-change='changePageSize' />
      </Col>
    </Row>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getPointLists, closePoint, delPoint } from '@/api/market/point.js'
import enumsMixin from '../enums.js'
export default {
  name: 'market_point_list',
  mixins: [enumsMixin],
  data () {
    return {
      tableHeight: 0,
      dropVisible: false,
      keyName: '',
      searchObj: {
        keyName: '',
        activityIntegerType: -1,
        activityStatus: -1
      },
      total: 0,
      pageSize: 10,
      pageIndex: 1,
      loading: false,
      columns: [
        {
          title: '活动编号',
          key: 'activityCode'
        },
        {
          title: '活动名称',
          key: 'keyName'
        },
        {
          title: '场景类型',
          slot: 'activityIntegerType'
        },
        {
          title: '活动开始时间',
          key: 'activityStart'
        },
        {
          title: '活动结束时间',
          key: 'activityEnd'
        },
        {
          title: '活动状态',
          slot: 'activityStatus'
        },
        {
          title: '创建时间',
          key: 'createTime'
        }
      ],
      data: []
    }
  },
  watch: {
    '$route' (newRoute) {
      if (newRoute.name === 'market_point_list') {
        this.search()
      }
    }
  },
  computed: {
    ...mapState({
      merchantId: state => state.user.merchantId
    })
  },
  mounted () {
    this.getData()
    if (this.hasPermission(['moms:market:pointscenecreate', 'moms:market:pointsceneupdate', 'moms:market:pointscenedelete', 'moms:market:pointscenedetail'])) {
      this.columns.push({
        title: '操作',
        fixed: 'right',
        slot: 'action',
        width: 160
      })
    }
  },
  methods: {
    filterSpace () {
      this.keyName = this.keyName.replace(/\s+/g, '')
    },
    skipAdd (id) {
      this.$router.push({ name: 'market_point_create', params: { type: id } })
    },
    skipEdit (row) {
      this.$router.push({
        name: 'market_point_edit',
        params: {
          id: row.activityIntegerId,
          type: row.activityIntegerType
        }
      })
    },
    skipDetail (row) {
      this.$router.push({
        name: 'market_point_detail',
        params: {
          id: row.activityIntegerId
        },
        query: {
          type: row.activityIntegerType
        }
      })
    },
    skipOver (row) {
      closePoint({
        // merchantId: this.merchantId,
        activityIntegerId: row.activityIntegerId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.$Message.success('结束成功！')
          this.getData()
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    getData () {
      this.loading = true
      this.tableHeight = 0
      var params = {
        merchantId: this.merchantId,
        keyName: this.searchObj.keyName,
        activityStatus: this.searchObj.activityStatus === -1 ? '' : this.searchObj.activityStatus,
        activityIntegerType: this.searchObj.activityIntegerType === -1 ? '' : this.searchObj.activityIntegerType,
        offset: (this.pageIndex - 1) * this.pageSize,
        limit: this.pageSize
      }
      getPointLists(params).then(res => {
        this.loading = false
        const result = res.data || {}
        if (result.status === 200) {
          this.data = result.data || []
          this.total = result.totalCount || 0
          if (this.data.length > 10) {
            this.tableHeight = 520
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
        this.loading = false
      })
    },
    sureDel (row) {
      delPoint({
        activityIntegerId: row.activityIntegerId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.$Message.success('删除成功！')
          setTimeout(() => {
            this.getData()
          }, 300)
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    search () {
      this.pageIndex = 1
      this.getData()
    },
    doSearch () {
      this.searchObj.keyName = this.keyName
      this.search()
    },
    reset () {
      this.keyName = ''
      this.searchObj = {
        keyName: '',
        activityIntegerType: -1,
        activityStatus: -1
      }
      this.search()
    },
    changePageIndex (page) {
      this.pageIndex = page
      this.getData()
    },
    changePageSize (size) {
      this.pageSize = size
      this.search()
    }
  }
}
</script>
